package com.weds.edjk.service;

import com.weds.core.base.BaseService;
import com.weds.edjk.mapper.FileMonitorMapper;
import com.weds.edjk.util.DateUtils;
import com.weds.edjk.vo.DepTreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author zcz
 * @Description 人员基本信息管理
 * @Date 2020-6-11
 */
@Service
public class FileMonitorService extends BaseService {

    @Autowired
    FileMonitorMapper fileMonitorMapper;
    /**
     * 头部  总人数、各子部门人数、性别
     * @return
     */
    public Map<String,Object> getHeadCount(String userDept){
        HashMap<String, Object> map = new HashMap<>();
        // 总人数
        int userCount = fileMonitorMapper.selectUserCountByDep(userDept);
        // 查询子部门
        List<Map<String, Object>> depMap = fileMonitorMapper.selectDepByParent(userDept);
        // 部门名列表
        List<String> depNames = new ArrayList<>();
        // 各个部门下的部门人数
        List<Map<String, Object>> depData = new ArrayList<>();
        depMap.forEach(d -> {
            int count = fileMonitorMapper.selectUserCountByDep(d.get("depSerial")+"");
            depNames.add(d.get("depName")+"");
            Map<String, Object> dep = new HashMap<>();
            dep.put("value",count);
            dep.put("name",d.get("depName")+"");
            depData.add(dep);
        });
        List<Map<String, String>> sexMap = fileMonitorMapper.selectUserCountByDepGroupBySex(userDept);
        map.put("userCount",userCount);
        map.put("depName",depNames);
        map.put("depData",depData);
        map.put("sexMap",sexMap);
        return map;
    }

    /**
     * 部门树
     * @return
     */
    public List<DepTreeVo> getDepTree(){
        List<DepTreeVo> depTreeVos = fileMonitorMapper.selectDepTreeByParent("0");
        for (DepTreeVo dep:depTreeVos){
            getDepTree(dep);
        }
        return depTreeVos;
    }

    /**
     * 递归查询部门
     * @param depTreeVo
     * @return
     */
    private DepTreeVo getDepTree(DepTreeVo depTreeVo){
        List<DepTreeVo> depTreeVos = fileMonitorMapper.selectDepTreeByParent(depTreeVo.getDepSerial());
        for (DepTreeVo dep:depTreeVos){
            getDepTree(dep);
        }
        depTreeVo.setChild(depTreeVos);
        return depTreeVo;
    }

    /**
     * 学历
     * @param userDept
     * @return
     */
    public Map<String, Object> getXueLiStatis(String userDept){
        Map<String, Object> map = new HashMap<>();
        List<String> xueLiName = new ArrayList<String>() {
            {
                this.add("小学");
                this.add("初中");
                this.add("高中");
                this.add("大专");
                this.add("本科");
                this.add("研究生");
                this.add("博士");
                this.add("15年以上");
            }
        };
        List<Integer> xueLiCount = new ArrayList<>();
        xueLiName.forEach(name -> {
                int count = fileMonitorMapper.selectCountByDepAndXueLi(userDept, name);
                xueLiCount.add(count);
        });
        map.put("xueLiName",xueLiName);
        map.put("xueLiCount",xueLiCount);
        return map;
    }

    /**
     * 工种
     * @param userDept
     * @return
     */
    public Map<String, Object> getWorkTypeStatis(String userDept){
        Map<String, Object> map = new HashMap<>();
        List<String> workTypeName = new ArrayList<String>() {
            {
                this.add("员工");
                this.add("组长");
                this.add("中层管理");
                this.add("高层管理");
            }
        };
        List<Integer> workTypeCount = new ArrayList<>();
        workTypeName.forEach(name -> {
            int count = fileMonitorMapper.selectCountByDepAndFname(userDept,name);
            workTypeCount.add(count);
        });
        map.put("workTypeName",workTypeName);
        map.put("workTypeCount",workTypeCount);
        return map;
    }

    /**
     * 获取各工作年限人数
     * @param userDept
     * @return
     */
    public Map<String, Object> getWorkdayStatis(String userDept){
        HashMap<String, Object> map = new HashMap<>();
        List<String> ageName = new ArrayList<String>() {
            {
                this.add("1年以下");
                this.add("1-2年");
                this.add("2-3年");
                this.add("3-5年");
                this.add("5-8年");
                this.add("8-10年");
                this.add("10-15年");
                this.add("15年以上");
            }
        };
        List<Integer> ageCount = new ArrayList<>();
        // 1年以下
        int oneCount = 0;
        // 2年以下
        int twoCount = 0;
        // 3年以下
        int threeCount = 0;
        // 5年以下
        int fiveCount = 0;
        // 8年以下
        int eightCount = 0;
        // 10年以下
        int tenCount = 0;
        // 15年以下
        int fifteenCount = 0;
        // 50年以下
        int fiftyCount = 0;
        List<Date> dates = fileMonitorMapper.selectWorkdayByDep(userDept);
        for (Date date:dates){
            int age = DateUtils.getWorkAge(date);
            int num = (((age<=1)==true?1:0)
                    +((age<=2)==true?1:0)
                    +((age<=3)==true?1:0)
                    +((age<=5)==true?1:0)
                    +((age<=8)==true?1:0)
                    +((age<=10)==true?1:0)
                    +((age<=15)==true?1:0)
                    +((age<=100)==true?1:0));
            switch(num){
                case 1:oneCount ++;break;
                case 2:twoCount ++;break;
                case 3:threeCount ++;break;
                case 4:fiveCount ++;break;
                case 5:eightCount ++;break;
                case 6:tenCount ++;break;
                case 7:fifteenCount ++;break;
                case 8:fiftyCount ++;break;
            }
        }
        ageCount.add(oneCount);
        ageCount.add(twoCount);
        ageCount.add(threeCount);
        ageCount.add(fiveCount);
        ageCount.add(eightCount);
        ageCount.add(tenCount);
        ageCount.add(fifteenCount);
        ageCount.add(fiftyCount);
        map.put("ageName",ageName);
        map.put("ageCount",ageCount);
        return map;
    }

    /**
     * 获取各年龄人数
     * @param userDept
     * @return
     */
    public Map<String, Object> getAgeStatis(String userDept){
        HashMap<String, Object> map = new HashMap<>();
        List<String> ageName = new ArrayList<String>() {
            {
                this.add("0-18");
                this.add("19-25");
                this.add("26-30");
                this.add("31-35");
                this.add("36-40");
                this.add("40-45");
                this.add("46-50");
                this.add("50+");
            }
        };
        List<Integer> ageCount = new ArrayList<>();
        // 18岁及以下
        int eighteenCount = 0;
        // 25岁及以下
        int twentyFiveCount = 0;
        // 30岁及以下
        int thirtyCount = 0;
        // 35岁及以下
        int thirtyFiveCount = 0;
        // 40岁及以下
        int fortyCount = 0;
        // 45岁及以下
        int fortyFiveCount = 0;
        // 50岁及以下
        int fiftyCount = 0;
        // 100岁及以下
        int hundredCount = 0;
        List<Date> dates = fileMonitorMapper.selectBirthdayByDep(userDept);
        for (Date date:dates){
            int age = DateUtils.getAge(date);
            int num = (((age<=18)==true?1:0)
                    +((age<=25)==true?1:0)
                    +((age<=30)==true?1:0)
                    +((age<=35)==true?1:0)
                    +((age<=40)==true?1:0)
                    +((age<=45)==true?1:0)
                    +((age<=50)==true?1:0)
                    +((age<=100)==true?1:0));
            switch(num){
                case 1:eighteenCount ++;break;
                case 2:twentyFiveCount ++;break;
                case 3:thirtyCount ++;break;
                case 4:thirtyFiveCount ++;break;
                case 5:fortyCount ++;break;
                case 6:fortyFiveCount ++;break;
                case 7:fiftyCount ++;break;
                case 8:hundredCount ++;break;
            }
        }
        ageCount.add(eighteenCount);
        ageCount.add(twentyFiveCount);
        ageCount.add(thirtyCount);
        ageCount.add(thirtyFiveCount);
        ageCount.add(fortyCount);
        ageCount.add(fortyFiveCount);
        ageCount.add(fiftyCount);
        ageCount.add(hundredCount);
        map.put("ageName",ageName);
        map.put("ageCount",ageCount);
        return map;
    }

}
